﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;

namespace KPIS.GERP.GHRMS.DAL
{
    public class HisEnglishDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public HisEnglishDAL()
        {

        }
        public DataSet LoadHisEnglishTest(Nullable<int> Sequence, Nullable<int> EmpSequence)
        {
            string strSQL;

            DataSet ds = null;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@ActiveRecord", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(1, "@Sequence", Sequence);
                dbManager.AddParameters(2, "@EmpSequence", EmpSequence);

                strSQL = "SELECT t.*"
                    + " FROM HRM_PS_EMP_ENGLISH t"
                    + " WHERE t.RECORD_STATUS = @ActiveRecord"
                    + (Sequence.HasValue ? " AND t.ENG_SEQ = @Sequence" : "")
                    + (EmpSequence.HasValue ? " AND t.EMP_SEQ = @EmpSequence" : "");

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);

            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
            return ds;
        }
        public void Insert(Nullable<int> EmpSequence, string InstitutesName, Nullable<DateTime> StartDate, Nullable<DateTime> EndDate
            , string Remarks, int CreatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(8);

                dbManager.AddParameters(0, "@EmpSequence", EmpSequence);
                dbManager.AddParameters(1, "@InstitutesName", InstitutesName);
                dbManager.AddParameters(2, "@StartDate", StartDate);
                dbManager.AddParameters(3, "@EndDate", EndDate);
                dbManager.AddParameters(4, "@Remarks", Remarks);
                dbManager.AddParameters(5, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(6, "@CreatedBy", CreatedBy);
                dbManager.AddParameters(7, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO HRM_PS_EMP_ENGLISH"
                    + "(EMP_SEQ, INS_NAME, DATE_START, DATE_END"
                        + ", REMARKS, RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES (@EmpSequence, @InstitutesName, @StartDate, @EndDate"
                        + ", @Remarks, @RecordStatus, @CreatedBy, @CreatedWhen)";
                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(int Sequence, string InstitutesName, Nullable<DateTime> StartDate, Nullable<DateTime> EndDate
            , string Remarks, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(7);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@InstitutesName", InstitutesName);
                dbManager.AddParameters(2, "@StartDate", StartDate);
                dbManager.AddParameters(3, "@EndDate", EndDate);
                dbManager.AddParameters(4, "@Remarks", Remarks);
                dbManager.AddParameters(5, "@UpdatedBy", UpdatedBy);
                dbManager.AddParameters(6, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_PS_EMP_ENGLISH"
                    + " SET INS_NAME = @InstitutesName"
                    + ", DATE_START = @StartDate"
                    + ", DATE_END = @EndDate"
                    + ", REMARKS = @Remarks"
                    + ", UPDATED_BY = @UpdatedBy"
                    + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE ENG_SEQ = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(int Sequence, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@RecordStatus", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@UpdatedBy", UpdatedBy);
                dbManager.AddParameters(3, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_PS_EMP_ENGLISH"
                    + " SET RECORD_STATUS = @RecordStatus"
                    + ", UPDATED_BY = @UpdatedBy"
                    + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE ENG_SEQ = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
